home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-08-17 | 5.2 KB | 220 lines |
- package jre.demo;
-
- import java.awt.*;
- import java.awt.image.*;
- import java.awt.event.*;
- import java.net.URL;
- import java.io.*;
-
- public class HelloWorld extends Frame
- implements ImageObserver, ActionListener, WindowListener {
-
- Button OKbutton = new Button("OK");
- MenuItem about = new MenuItem("About...");
- static final int HEIGHT = 500;
- static final int WIDTH = 500;
- boolean imageHosed = false;
-
- /* The about Dialog and OK button */
- Button aboutOK;
- Dialog aboutD;
-
- Image img;
-
- public void repaint() {
- super.repaint();
- showImage();
- }
-
- public void repaint(long tm) {
- super.repaint(tm);
- showImage();
- }
-
- public void repaint(int x, int y, int w, int h) {
- super.repaint(x, y, w, h);
- showImage();
- }
-
- public void repaint(long tm, int x, int y, int w, int h) {
- super.repaint(tm, x, y, w, h);
- showImage();
- }
-
- public void update(Graphics g) {
- super.update(g);
- showImage();
- }
-
- public void paint(Graphics g) {
- super.paint(g);
- showImage();
- }
-
- public /* */ HelloWorld(String title) {
- super(title);
- setLayout(new BorderLayout(10, 10));
- // setLayout(new FlowLayout());
- setResizable(false);
- setBackground(Color.darkGray);
- OKbutton.addActionListener(this);
- addWindowListener(this);
- // OKbutton.setSize(100, 50);
- Panel p = new Panel();
- p.add(OKbutton);
- MenuBar mb = new MenuBar();
- // mb.setBackground(Color.lightGray);
- Menu me = new Menu("Help");
- // me.setBackground(Color.lightGray);
- // about.setBackground(Color.lightGray);
- me.add(about);
- about.addActionListener(this);
- mb.add(me);
- setMenuBar(mb);
- add(p, BorderLayout.SOUTH);
- pack();
- }
-
- static void p(String s) {
- System.out.println(s);
- }
-
- public boolean imageUpdate(Image img, int info,
- int x, int y, int w, int h) {
- Graphics g = getGraphics();
- if (g != null) {
- g.drawImage(img, 20, 20, WIDTH - 100, HEIGHT - 200, this);
- }
- return (info & (ALLBITS | ERROR | ABORT)) == 0;
- }
-
- /** ActionListener method. */
-
- public void actionPerformed(ActionEvent e) {
- Object src = e.getSource();
- if (src == OKbutton) {
- System.exit(0);
- } else if (src == about) {
- aboutD = new Dialog(this, "About", true);
- aboutD.setSize(200, 200);
- TextArea txt = new TextArea("", 30, 8,
- TextArea.SCROLLBARS_NONE);
- txt.setBackground(Color.lightGray);
- txt.setEditable(false);
- txt.setText("Hello World, using the\n"+
- "minimal Java\nRuntime Environment.\n"+
- "\nv. 1.0\n\nDave Brown");
- aboutD.add("Center", txt);
- Panel p = new Panel();
- p.setBackground(Color.lightGray);
- aboutOK = new Button("OK");
- p.add(aboutOK);
- aboutOK.addActionListener(this);
- // aboutD.add(aboutOK);
- aboutD.add("South", p);
- Point po = getLocation();
- aboutD.setLocation(po.x + WIDTH + 5, po.y);
- aboutD.setVisible(true);
- } else if (src == aboutOK && aboutOK != null) {
- aboutD.dispose();
- aboutD = null;
- aboutOK = null;
- }
- }
-
- /** WindowListener methods */
-
- public void windowActivated(WindowEvent e) {
- setVisible(true);
- showImage();
- }
-
- public void windowClosed(WindowEvent e) {}
-
- public void windowClosing(WindowEvent e) {}
-
- public void windowDeactivated(WindowEvent e) {
- showImage();
- }
-
- public void windowDeiconified(WindowEvent e) {
- showImage();
- }
-
- public void windowIconified(WindowEvent e) { }
-
- public void windowOpened(WindowEvent e) {}
-
- void loadImage() throws IOException {
- URL imgURL;
-
- String jh = System.getProperty("java.home");
- String file = jh + File.separator+"lib"+
- File.separator+"smooch.gif";
- // see if it's there
- if (!(new File(file)).exists()) {
- throw new FileNotFoundException(file);
- }
- imgURL = new URL("file", "", file);
- ImageProducer prod = (ImageProducer)imgURL.getContent();
- img = Toolkit.getDefaultToolkit().createImage(prod);
- }
-
- void showErrorAndExit(Exception e) {
- String jh = System.getProperty("java.home");
- String file = jh + File.separator+"lib"+
- File.separator+"smooch.gif";
- TextArea txt = new TextArea("", 30, 20);
- txt.setBackground(Color.lightGray);
- txt.setEditable(false);
- OutputStream os = new ByteArrayOutputStream();
- PrintStream ps = new PrintStream(os);
- e.printStackTrace(ps);
- txt.setText("Can't load image from:\n"+
- file+":\n\n"+os.toString());
- add("Center", txt);
- setTitle("ERROR");
- }
-
- synchronized void showImage() {
- if (imageHosed) {
- return;
- }
- if (img == null) {
- try {
- loadImage();
- } catch (IOException e) {
- imageHosed = true;
- System.err.println("can't load image: " + e);
- e.printStackTrace();
- showErrorAndExit(e);
- return;
- }
- }
-
- Graphics g = getGraphics();
- if (g == null) {
- return;
- }
- g.drawImage(img, 20, 20, WIDTH - 100, HEIGHT - 200, this);
- g.setColor(Color.white);
- g.drawString("Hello World", 200, HEIGHT - 100);
- }
-
- static {
- System.loadLibrary("HelloWorld");
- }
-
- /* native method, for purposes of demonstration */
- private static native void nativeMethod(String s);
-
- public static void main(String[] a) throws IOException {
- String title = (a.length == 0) ? "Howdy": a[0];
- nativeMethod(title);
- HelloWorld w = new HelloWorld(title);
- w.setSize(WIDTH, HEIGHT);
- w.setVisible(true);
- }
- }
-
-